//
//  BYCashierTypeView.m
//  OCDemo
//
//  Created by zhumuzhi on 2022/2/17.
//

#import "BYCashierTypeView.h"


@interface BYCashierTypeView ()

@property (nonatomic, strong) UIImageView   *icon;

@property (nonatomic, strong) UILabel       *title;

@property (nonatomic, strong) UIButton      *seleced;

@end

@implementation BYCashierTypeView

#pragma mark - Event
- (void)selectedEvent {
    if(self.selectedBlock) {
        self.selectedBlock(self.payType);
    }
}

- (void)selectedMethod {
    self.seleced.selected = YES;
}

- (void)unSelectedMethod {
    self.seleced.selected = NO;
}

#pragma mark - SetData
- (void)setPayType:(BYCashierType)payType {
    _payType = payType;
    
    if (BYCashierTypeAli == payType) {
        self.title.text = @"支付宝";
        self.icon.image = [UIImage imageNamed:@"pay_alipay"];
    }else {
        self.title.text = @"微信";
        self.icon.image = [UIImage imageNamed:@"pay_wechat"];
    }
}

#pragma mark - Init

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self setViewCornerRadius:kAutoWithSize(10)];
        
        [self addSubview:self.icon];
        [self addSubview:self.title];
        [self addSubview:self.seleced];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.equalTo(self).offset(kAutoWithSize(10));
        make.bottom.equalTo(self).offset(-kAutoWithSize(10));
        make.width.mas_equalTo(kAutoWithSize(60));
    }];
    
    [self.title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.icon.mas_right).offset(kAutoWithSize(10));
        make.centerY.equalTo(self.icon);
    }];
    
    [self.seleced mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self);
        make.right.equalTo(self.mas_right).offset(-kAutoWithSize(10));
        make.width.height.mas_equalTo(kAutoWithSize(15));
    }];
}


#pragma mark - LazyGet
- (UIImageView *)icon {
    if (!_icon) {
        _icon = [[UIImageView alloc] init];
    }
    return _icon;
}

- (UILabel *)title {
    if (!_title) {
        _title = [[UILabel alloc] init];
    }
    return _title;
}

-(UIButton *)seleced {
    if (!_seleced) {
        _seleced = [[UIButton alloc] init];
        [_seleced setImage:[UIImage imageNamed:@"check_selected"] forState:UIControlStateSelected];
        [_seleced setImage:[UIImage imageNamed:@"check_normal"] forState:UIControlStateNormal];
        [_seleced addTarget:self action:@selector(selectedEvent) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seleced;
}


@end
